package com.hua.fanqiemall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hua.fanqiemall.product.dao.BrandDao;
import com.hua.fanqiemall.product.dao.CategoryDao;
import com.hua.fanqiemall.product.entity.BrandEntity;
import com.hua.fanqiemall.product.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hua.common.utils.PageUtils;
import com.hua.common.utils.Query;

import com.hua.fanqiemall.product.dao.CategoryBrandRelationDao;
import com.hua.fanqiemall.product.entity.CategoryBrandRelationEntity;
import com.hua.fanqiemall.product.service.CategoryBrandRelationService;

/**
 * 分类品牌关联服务
 *
 * @author luoweihua
 * @email lwh_559@163.com
 * @date 2022/1/12 15:07
 */
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryDao categoryDao;

    /**
     * 原始分页查询-查询分类品牌关联信息
     * @author luoweihua
     * @date 2022/1/12 15:06
     * @param params 查询参数
     * @return PageUtils
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存分类、品牌关联详情接口
     * @author luoweihua
     * @date 2022/1/12 15:05
     * @param categoryBrandRelation 分类、品牌关联详情
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);

        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        categoryBrandRelation.setBrandName(brandEntity.getName());
        this.save(categoryBrandRelation);
    }

    /**
     * 更新关联品牌接口
     * @author luoweihua
     * @date 2022/1/12 15:04
     * @param brandId 品牌ID
     * @param name 品牌名
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setBrandId(brandId);
        entity.setBrandName(name);
        this.update(entity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
    }

    /**
     * 更新关联分类接口
     * @author luoweihua
     * @date 2022/1/12 15:02
     * @param catId 分类ID
     * @param name 分类名
     */
    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId , name);
    }

    /**
     * 根据分类ID获取品牌信息接口
     * @author luoweihua
     * @date 2022/1/12 15:01
     * @param catId 分类ID
     * @return List<BrandEntity>
     */
    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<BrandEntity> brandEntityList = new ArrayList<>();
        List<CategoryBrandRelationEntity> relationEntities = this.baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        for (CategoryBrandRelationEntity relationEntity : relationEntities) {
            BrandEntity brandEntity = brandDao.selectById(relationEntity.getBrandId());
            brandEntityList.add(brandEntity);
        }
        return brandEntityList;
    }

}